package com.taotaojs.myboot.task;

import com.taotaojs.myboot.bean.DemoOracleBean;
import com.taotaojs.myboot.bean.PushInfoModel;
import com.taotaojs.myboot.bean.UserModel;
import com.taotaojs.myboot.service.DemoOracleServiceImpl;
import com.taotaojs.myboot.service.PushInfoService;
import com.taotaojs.myboot.service.UserService;
import com.taotaojs.util.CollectionUtil;
import com.taotaojs.util.ConstantUtil;
import com.taotaojs.util.StringUtil;
import com.taotaojs.util.ThreadPoolUtil;
import com.taotaojs.util.date.CalendarUtil;
import com.taotaojs.util.number.NumberFormatUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


//@Transactional
/**
 * @author hantao
 */
@Component
@Log4j2
public class ScheduledTask {

    @Autowired
    private PushInfoService pushInfoService;

    @Autowired
    private DemoOracleServiceImpl demoOracleService;

    @Autowired
    private UserService userService;

    private List<PushInfoModel> pushInfoList = new ArrayList<>();
    private int index;

    @Scheduled(cron = "0 * * * * ?")
    public void pushInfoByMessageBegin() {

        //获取当前分钟
        Date date = new Date();
        Date startDate = CalendarUtil.getLastOrNextMinute(date, -1);
        Date stopDate = CalendarUtil.getLastOrNextMinute(date, 0);
        //根据时间段获取所有列表
        List<PushInfoModel> pushInfoModelList = pushInfoService.findByStartAndStopDate(startDate, stopDate, "create_time");

        if(CollectionUtil.isEmpty(pushInfoModelList)) {
            return;
        }

        List<Integer> userIds = CollectionUtil.getPros(pushInfoModelList, PushInfoModel.class, "userId");
        Map<Integer, UserModel> userMap = userService.findByObjMap(userIds);

        List<DemoOracleBean> demoOracleBeanList = new ArrayList<>();
        for (PushInfoModel pushInfoModel : pushInfoModelList) {
//            pushInfoModel.getUserId().equals()
            Integer userId = NumberFormatUtil.stringToInteger(pushInfoModel.getUserId());
            String phone = null;
            for (Map.Entry<Integer, UserModel> integerUserModelEntry : userMap.entrySet()) {
                if(userId.equals(integerUserModelEntry.getKey())) {
                    phone = userMap.get(integerUserModelEntry.getKey()).getPhone();
                    break;
                }
            }
            if(StringUtil.isBlank(phone)) {
                continue;
            }
            demoOracleBeanList.add(DemoOracleBean.builder()
                    .mstype("行迪评审系统")
                    .orgAddr("106904584456")
                    .destAddr(phone)
                    .content("【丽水市中心医院】" + pushInfoModel.getMessage())
                    .mszt(0)
                    .build());
        }

        //需要更新的数据集合
        for (PushInfoModel pushInfoModel : pushInfoModelList) {
            PushInfoModel pushInfo = new PushInfoModel();
            pushInfo.setId(pushInfoModel.getId());
            pushInfo.setEnabled(true);
            this.pushInfoList.add(pushInfo);
        }

        log.info(demoOracleBeanList);

        demoOracleService.batchInsertObject(demoOracleBeanList);

        //分批次批量更新
        if(++index > ConstantUtil.THOUSAND || this.pushInfoList.size() > ConstantUtil.BEST ){
            if(CollectionUtil.isEmpty(this.pushInfoList)) {
                ThreadPoolUtil threadPoolUtil = new ThreadPoolUtil(ThreadPoolUtil.FIXED_THREAD, 5);
                threadPoolUtil.execute(() -> {
                    pushInfoService.batchUpdateObject(this.pushInfoList);
                    index = 0;
                    this.pushInfoList.clear();
                });
            }else{
                index = 0;
            }
        }

    }

}
